package com.xx.controller.xunyou.app;

import com.xx.controller.util.MyCachUtil;
import com.xx.springBootUtil.beanUtils.BeanUtilsExtends;
import com.xx.springBootUtil.excption.BaseException;
import com.xx.springBootUtil.result.JsonResult;
import com.xx.springBootUtil.util.ObjectHelper;
import com.xx.springBootUtil.validate.ValidateWriteIn;
import com.xx.xunyou.entity.item.XunyouItemType;
import com.xx.xunyou.entity.item.chizhuzaixiang.Chizhuzaixiang;
import com.xx.xunyou.entity.item.chizhuzaixiang.ChizhuzaixiangContent;
import com.xx.xunyou.entity.item.chizhuzaixiang.vo.ChizhuzaixiangContentVo;
import com.xx.xunyou.entity.item.chizhuzaixiang.vo.ChizhuzaixiangInfoVo;
import com.xx.xunyou.entity.item.chizhuzaixiang.vo.ChizhuzaixiangPageVo;
import com.xx.xunyou.entity.item.chizhuzaixiang.vo.ChizhuzaixiangVo;
import com.xx.xunyou.entity.item.good.XunyouGood;
import com.xx.xunyou.entity.item.vo.XunyouItemTypeTopAndSonsVo;
import com.xx.xunyou.entity.item.vo.XunyouItemTypeVo;
import com.xx.xunyou.entity.user.vo.XunyouUserBaseVo;
import com.xx.xunyou.service.ChizhuzaixiangService;
import com.xx.xunyou.service.ItemTypeService;
import com.xx.xunyou.service.XunyouGoodService;
import com.xx.xunyou.service.XunyouUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.*;

/**
 * @program: xunyou-parent
 * @author: zhl
 * @create: 2019-05-03
 **/
@Api(value = "API - APPChizhuzaixiangController", description = "吃住在乡村")
@RestController
public class APPChizhuzaixiangController {

    public static final String ParentTypeKey = "chizhuzaixiangcun";

    @Autowired
    private ChizhuzaixiangService chizhuzaixiangService;

    @Autowired
    private XunyouUserService xunyouUserService;

    @Autowired
    private ItemTypeService itemTypeService;

    @Autowired
    private XunyouGoodService xunyouGoodService;



    @ApiOperation(value = "吃住在乡村查询", notes = "查询", response = ChizhuzaixiangPageVo.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "typeKey", value = "产品类型关键字", dataType = "String", required = false, paramType = "query"),
            @ApiImplicitParam(name = "pageIndex", value = "开始页   1开始 默认 1  ", dataType = "string", required = false, paramType = "query"),
            @ApiImplicitParam(name = "pageSize", value = "展示条数", dataType = "string", required = false, paramType = "query")
    })
    @PostMapping("app/chizhuzaixiang/query")
    public JsonResult query(String typeKey,
                            @RequestParam(name = "pageIndex", defaultValue = "1") Integer pageIndex,
                            @RequestParam(name = "pageSize", defaultValue = "10") Integer pageSize) {

        Map<String, Object> map = new HashMap<>();

        int start = pageSize * ((pageIndex < 1 ? 1 : pageIndex) - 1);
        map.put("start", start);
        map.put("pageIndex", pageIndex);
        map.put("pageSize", pageSize);
        ChizhuzaixiangPageVo chizhuzaixiangPageVo = new ChizhuzaixiangPageVo();

        //查询分类
        XunyouItemTypeTopAndSonsVo xunyouItemTypeTopAndSonsVo = itemTypeService.queryItemTypeTopAndSonsVoByTypeKey(ParentTypeKey, typeKey);
        chizhuzaixiangPageVo.setXunyouItemTypeTopAndSonsVo(xunyouItemTypeTopAndSonsVo);
        List<XunyouItemTypeVo> itemTypeSons = xunyouItemTypeTopAndSonsVo.getItemTypeSons();
        if (ObjectHelper.isNotEmpty(itemTypeSons)) {
            StringBuffer itemIds = new StringBuffer();
            itemIds.append("(");
            for (int i = 0; i < itemTypeSons.size(); i++) {
                if (i == (itemTypeSons.size() - 1)) {
                    itemIds.append(itemTypeSons.get(i).getTypeId());
                } else {
                    itemIds.append(itemTypeSons.get(i).getTypeId() + ", ");
                }
            }

            itemIds.append(")");
            map.put("itemIds", itemIds.toString());

            //查询总条数
            Integer total = chizhuzaixiangService.querByMapToAPPTotal(map);
            chizhuzaixiangPageVo.setTotal(total);

            if (total > 0) {
                List<ChizhuzaixiangVo> recommendVos = new ArrayList<>();
                if (itemTypeSons.size() > 1) {
                    //查询推荐数据
                    queryRecommendVos(map, recommendVos);
                    chizhuzaixiangPageVo.setRecommendVos(recommendVos);
                }

                //查询页面数据
                List<Chizhuzaixiang> chizhuzaixiangs1 = chizhuzaixiangService.querByMapToAPPPage(map);
                List<ChizhuzaixiangVo> chizhuzaixiangVos = new ArrayList<>();
                if (ObjectHelper.isNotEmpty(chizhuzaixiangs1)) {
                    for (Chizhuzaixiang chizhuzaixiang : chizhuzaixiangs1) {
                        ChizhuzaixiangVo chizhuzaixiangVo = new ChizhuzaixiangVo();
                        BeanUtilsExtends.copyProperties(chizhuzaixiangVo, chizhuzaixiang);
                        chizhuzaixiangVos.add(chizhuzaixiangVo);
                    }
                }
                chizhuzaixiangPageVo.setChizhuzaixiangVos(chizhuzaixiangVos);
            }
        }
        return new JsonResult(chizhuzaixiangPageVo);
    }

    /**
     * 查询推荐
     * @param map
     * @param recommendVos
     */
    private void queryRecommendVos(Map<String, Object> map, List<ChizhuzaixiangVo> recommendVos) {
        List<Chizhuzaixiang> recommend = chizhuzaixiangService.querByMapToAPPPageRecommend(map);
        if (ObjectHelper.isNotEmpty(recommend)) {
            for (Chizhuzaixiang chizhuzaixiang : recommend) {
                ChizhuzaixiangVo chizhuzaixiangVo = new ChizhuzaixiangVo();
                BeanUtilsExtends.copyProperties(chizhuzaixiangVo, chizhuzaixiang);
                recommendVos.add(chizhuzaixiangVo);
            }
        }
    }

    /**
     * 吃住在乡村查询详情
     * @param token
     * @param chizhuzaixiangId
     * @return
     */
    @ApiOperation(value = "吃住在乡村查询详情", notes = "吃住在乡村查询详情", response = ChizhuzaixiangInfoVo.class)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "chizhuzaixiangId", value = "吃住在乡村Id", dataType = "int", required = true, paramType = "query"),
            @ApiImplicitParam(name = "token", value = "登录唯一凭证", dataType = "string", required = false, paramType = "query"),

    })
    @ValidateWriteIn(parameter = {"chizhuzaixiangId"},msg = {"id不能为空"})
    @PostMapping("app/chizhuzaixiang/queryInfo")
    public JsonResult queryInfo(String token,
                                Integer chizhuzaixiangId) {

        Integer userId = MyCachUtil.getUserIdByToken(token);

        //查询基本信息
        Chizhuzaixiang chizhuzaixiang = chizhuzaixiangService.queryByIdToManager(chizhuzaixiangId);
        if(chizhuzaixiang==null){
            throw new BaseException("不存在！");
        }
        ChizhuzaixiangVo chizhuzaixiangVo = new ChizhuzaixiangVo();
        BeanUtilsExtends.copyProperties(chizhuzaixiangVo, chizhuzaixiang);

        //创建时间
        Date createTime = chizhuzaixiang.getCreateTime();

        //作者
        XunyouUserBaseVo author = xunyouUserService.queryAuthor(chizhuzaixiang.getUserId());

        //查询分类
        XunyouItemType xunyouItemType = itemTypeService.queryById(chizhuzaixiang.getTypeId());
        XunyouItemTypeTopAndSonsVo xunyouItemTypeTopAndSonsVo = itemTypeService.queryItemTypeTopAndSonsVoByTypeKey(ParentTypeKey, xunyouItemType.getTypeKey());

        //父分类
        XunyouItemTypeVo parentItemTypeVo = new XunyouItemTypeVo();
        BeanUtilsExtends.copyProperties(parentItemTypeVo, xunyouItemTypeTopAndSonsVo);
        List<XunyouItemTypeVo> itemTypeSons = xunyouItemTypeTopAndSonsVo.getItemTypeSons();

        //分类
        XunyouItemTypeVo itemTypeVo = new XunyouItemTypeVo();
        itemTypeVo = itemTypeSons.get(0);
        BeanUtilsExtends.copyProperties(itemTypeVo, xunyouItemType);

        Map<String, Object> map = new HashMap<>();
        map.put("itemIds", "("+itemTypeVo.getTypeId()+")");

        //推荐信息
        List<ChizhuzaixiangVo> recommendVos = new ArrayList<>();
        queryRecommendVos(map, recommendVos);

        //查询内容
        ChizhuzaixiangContent chizhuzaixiangContent = chizhuzaixiangService.queryContentByIdToManager(chizhuzaixiangId);
        ChizhuzaixiangContentVo chizhuzaixiangContentVo = new ChizhuzaixiangContentVo();
        BeanUtilsExtends.copyProperties(chizhuzaixiangContentVo, chizhuzaixiangContent);

        //被赞次数
        long goodNum = xunyouGoodService.queryCount(chizhuzaixiangId,xunyouItemType.getTypeId());

        //是否赞了
        Boolean isGood = false;
        if (userId!=null){
            XunyouGood xunyouGood = xunyouGoodService.queryByUserIdAndObjectIdAndTypeId(userId,chizhuzaixiangId,xunyouItemType.getTypeId());
            if (xunyouGood!=null){
                isGood = true;
            }
        }

        ChizhuzaixiangInfoVo chizhuzaixiangInfoVo = new ChizhuzaixiangInfoVo();
        chizhuzaixiangInfoVo.setAuthor(author);
        chizhuzaixiangInfoVo.setChizhuzaixiangContentVo(chizhuzaixiangContentVo);
        chizhuzaixiangInfoVo.setChizhuzaixiangVo(chizhuzaixiangVo);
        chizhuzaixiangInfoVo.setCreateTime(createTime);
        chizhuzaixiangInfoVo.setGoodNum(goodNum);
        chizhuzaixiangInfoVo.setItemTypeVo(itemTypeVo);
        chizhuzaixiangInfoVo.setParentItemTypeVo(parentItemTypeVo);
        chizhuzaixiangInfoVo.setRecommendVos(recommendVos);
        chizhuzaixiangInfoVo.setIsGood(isGood);
        return new JsonResult(chizhuzaixiangInfoVo);
    }
}
